package com.blog.controller;

import com.blog.pojo.Label;
import com.blog.pojo.User;
import com.blog.service.LabelService;
import com.blog.util.UUIdGenId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "label.api", produces = "application/json;charset=utf8")
@ResponseBody
@SuppressWarnings("all")
public class LabelController {



    @Autowired
    private LabelService service;

    Map<String, Object> code = new HashMap<>();

    /**
     * 添加标签
      * @param name
     * @param describe
     * @param session
     * @return
     */
    @RequestMapping("add")
    public Object add(String name, String describe,HttpSession session) {
        User user = (User) session.getAttribute("user");

        if(user == null) {
            code.put("code", "1");
            code.put("msg", "请登录");
            return code;
        }



        String uid = new UUIdGenId().genId("", "");
        boolean add = service.add(uid,user.getId(), name, describe);
        if (add) {
            Label label = new Label();
            label.setId(uid);
            label.setName(name);
            label.setDescribe(describe);
            code.put("code", "0");
            code.put("msg", "添加成功");
            code.put("data", label);
        } else {
            code.put("code", "1");
        }
        return code;
    }

    // 获取标签
    @RequestMapping("get")
    public Object get(Label label,Integer page,Integer limit,HttpSession session) {
        User user = (User) session.getAttribute("user");

        if(user == null) {
            code.put("code", "1");
            code.put("msg", "请登录");
            code.put("data", "{msg:'请登录'}");
            code.put("count", "0");
            return code;
        }

        // 修改对象中的id
        label.setUser_id(user.getId());


        // 设置 查询参数
        code.put("label",label);
        code.put("page",page-1);
        code.put("limit",limit);

        // 查询数量
        Integer count = service.getCount(code);

        // 查询数据
        List<Label> labels = service.get(code);
        System.out.println(labels.get(0).getCreate_date_time());
        // 查询结束 map 给空
        code = new HashMap<>();

        // 设置返回参数
        code.put("code", "0");
        code.put("count", count);
        code.put("data", labels);

        // 返回
        return code;
    }


    @PostMapping("delete")
    public Object delete(String id) {
        boolean delete = service.delete(id);
        if(delete){
            code.put("code","0");
            code.put("msg","删除成功");
        }else {
            code.put("code","1");
            code.put("msg","删除失败,请联系管理员");
        }
        return code;
    }

    @PostMapping("update")
    public Object update(Label label){

            Label label1 = new Label();
            label1.setId(label.getId());
            label1.setName(label.getName());
            label1.setDescribe(label.getDescribe());
            label1.setState(label.getState());

            code = new HashMap<>();

            code.put("label",label1);
            boolean update = service.update(code);
            if(update){
                code.put("code", "0");
                code.put("msg", "更新成功");
            }else {
                code.put("code", "0");
                code.put("msg", "更新失败,请联系管理员");
            }
         return code;
    }
}
